package com.huicoo.forestmanager.widget.tipsview;

import android.content.Context;
import android.support.annotation.LayoutRes;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.huicoo.forestmanager.R;
import com.huicoo.forestmanager.util.DisplayUtil;

import java.util.List;

/**
 * 自动匹配长度的标签容器
 */

public class TipsGroupView extends LinearLayout {

    private Context context;
    public boolean isShowAll = true;
    private boolean isSingleCheck;
    private int lr_small = -1, lr_big = -1, leftMargin = -1;

    public TipsGroupView(Context context) {
        super(context);
        this.context = context;
        setOrientation(VERTICAL);//设置方向
    }

    public TipsGroupView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        setOrientation(VERTICAL);//设置方向
    }

    public TipsGroupView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        setOrientation(VERTICAL);//设置方向
    }


    /**
     * 外部接口调用
     *
     * @param items
     * @param onItemClick
     */
    public void initViews(String items[], final OnItemClick onItemClick) {
        int length = getPaddingLeft() + getPaddingRight();//一行加载item 的宽度

        LinearLayout layout = null;

        LayoutParams layoutLp = null;

        boolean isNewLine = true;//是否换行

        int screenWidth = DisplayUtil.getScreenWidth(getContext());//屏幕的宽度

        int size = items.length;
        for (int i = 0; i < size; i++) {//便利items
            if (isNewLine) {//是否开启新的一行
                layout = new LinearLayout(context);
                layout.setOrientation(HORIZONTAL);
                layoutLp = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                layoutLp.topMargin = DisplayUtil.dp2px(context, 10);
            }

            View view = LayoutInflater.from(context).inflate(R.layout.widget_tips_item, null);
            TextView itemView = view.findViewById(R.id.text);
            itemView.setText(items[i]);
            int pdTB = DisplayUtil.dp2px(context, 8);
            int pdLR1 = lr_big == -1 ? DisplayUtil.dp2px(context, 12) : lr_big;
            int pdLR2 = lr_small == -1 ? pdTB : lr_small;
            if (items[i].length() <= 5) {
                itemView.setPadding(pdLR1, pdTB, pdLR1, pdTB);
            } else {
                itemView.setPadding(pdLR2, pdTB, pdLR2, pdTB);
            }

            final int j = i;
            itemView.setOnClickListener(new OnClickListener() {//给每个item设置点击事件
                @Override
                public void onClick(View v) {
                    if (null != onItemClick) {
                        onItemClick.onClick(j);
                    }
                }
            });

            //设置item的参数
            LayoutParams itemLp = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            int lm = this.leftMargin == -1 ? DisplayUtil.dp2px(context, 12) : this.leftMargin;
            itemLp.leftMargin = lm;

            //得到当前行的长度
            length += lm + getViewWidth(itemView);
            if (length > screenWidth) {//当前行的长度大于屏幕宽度则换行
                length = getPaddingLeft() + getPaddingRight();
                addView(layout, layoutLp);
                isNewLine = true;
                i--;
            } else {//否则添加到当前行
                isNewLine = false;
                layout.addView(view, itemLp);
            }
        }
        if (layout != null) {
            addView(layout, layoutLp);
        }
    }

    /**
     * 外部接口调用
     *
     * @param itemList
     * @param onItemClick
     */
    public void initViews(final List<TipsItem> itemList, final OnItemClickListenerV2 onItemClick) {
        int length = getPaddingLeft() + getPaddingRight();//一行加载item 的宽度

        LinearLayout layout = null;

        LayoutParams layoutLp = null;

        boolean isNewLine = true;//是否换行

        int screenWidth = DisplayUtil.getScreenWidth(getContext());//屏幕的宽度

        int size = itemList.size();
        for (int i = 0; i < size; i++) {//便利items
            if (isNewLine) {//是否开启新的一行
                layout = new LinearLayout(context);
                layout.setOrientation(HORIZONTAL);
                layoutLp = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                layoutLp.topMargin = DisplayUtil.dp2px(context, 10);
            }

            View view = LayoutInflater.from(context).inflate(R.layout.widget_tips_item, null);
            final TextView itemView = view.findViewById(R.id.text);
            itemView.setText(itemList.get(i).tipName);
            int pdTB = DisplayUtil.dp2px(context, 5);
            int pdLR1 = lr_big == -1 ? DisplayUtil.dp2px(context, 5) : lr_big;
            int pdLR2 = lr_small == -1 ? DisplayUtil.dp2px(context, 8) : lr_small;
            if (itemList.get(i).tipName.length() <= 5) {
                itemView.setPadding(pdLR1, pdTB, pdLR1, pdTB);
            } else {
                itemView.setPadding(pdLR2, pdTB, pdLR2, pdTB);
            }
            if (itemList.get(i).isChecked) {
                itemView.setBackgroundResource(R.drawable.bg_tips_border_check);
                itemView.setTextColor(ContextCompat.getColor(context, R.color.colorPrimary));
            } else {
                itemView.setBackgroundResource(R.drawable.bg_tips_border);
                itemView.setTextColor(ContextCompat.getColor(context, R.color.black));
            }

            final int j = i;
            itemView.setOnClickListener(new OnClickListener() {//给每个item设置点击事件
                @Override
                public void onClick(View v) {
                    if (isSingleCheck) {
                        for (int x = 0; x < itemList.size(); x++) {
                            itemList.get(x).isChecked = false;
                        }
                        for (int x = 0; x < getChildCount(); x++) {
                            LinearLayout ll_temp = (LinearLayout) getChildAt(x);
                            for (int y = 0; y < ll_temp.getChildCount(); y++) {
                                TextView tv_temp = (TextView) ll_temp.getChildAt(y);
                                tv_temp.setBackgroundResource(R.drawable.bg_tips_border);
                                tv_temp.setTextColor(ContextCompat.getColor(context, R.color.black));
                            }
                        }
                    }
                    TipsItem tempItem = itemList.get(j);
                    tempItem.isChecked = !tempItem.isChecked;
                    if (itemList.get(j).isChecked) {
                        itemView.setBackgroundResource(R.drawable.bg_tips_border_check);
                        itemView.setTextColor(ContextCompat.getColor(context, R.color.colorPrimary));
                    } else {
                        itemView.setBackgroundResource(R.drawable.bg_tips_border);
                        itemView.setTextColor(ContextCompat.getColor(context, R.color.black));
                    }
                    itemView.invalidate();
                    if (null != onItemClick) {
                        onItemClick.onClick(tempItem, tempItem.isChecked);
                    }
                }
            });

            //设置item的参数
            LayoutParams itemLp = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            int lm = this.leftMargin == -1 ? DisplayUtil.dp2px(context, 12) : this.leftMargin;
            itemLp.leftMargin = lm;

            //得到当前行的长度
            length += lm + getViewWidth(itemView);
            if (length > screenWidth) {//当前行的长度大于屏幕宽度则换行
                length = getPaddingLeft() + getPaddingRight();
                addView(layout, layoutLp);
                isNewLine = true;
                i--;
            } else {//否则添加到当前行
                isNewLine = false;
                layout.addView(view, itemLp);
            }
        }
        if (layout != null) {
            addView(layout, layoutLp);
        }
    }

    /**
     * 外部接口调用
     */
    public void initViews(final List<String> itemList, @LayoutRes int layoutRes) {
        int length = getPaddingLeft() + getPaddingRight();//一行加载item 的宽度

        LinearLayout layout = null;

        LayoutParams layoutLp = null;

        boolean isNewLine = true;//是否换行

        int screenWidth = DisplayUtil.getScreenWidth(getContext());//屏幕的宽度

        int size = itemList.size();
        for (int i = 0; i < size; i++) {//便利items
            if (isNewLine) {//是否开启新的一行
                layout = new LinearLayout(context);
                layout.setOrientation(HORIZONTAL);
                layoutLp = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            }

            TextView view = (TextView) LayoutInflater.from(context).inflate(layoutRes, null);
            view.setText(itemList.get(i));

            //设置item的参数
            LayoutParams itemLp = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            int lm = this.leftMargin == -1 ? DisplayUtil.dp2px(context, 10) : this.leftMargin;
            itemLp.rightMargin = lm;

            //得到当前行的长度
            length += lm + getViewWidth(view);
            if (length > screenWidth) {//当前行的长度大于屏幕宽度则换行
                length = getPaddingLeft() + getPaddingRight();
                addView(layout, layoutLp);
                isNewLine = true;
                i--;
            } else {//否则添加到当前行
                isNewLine = false;
                layout.addView(view, itemLp);
            }
        }
        if (layout != null) {
            addView(layout, layoutLp);
        }
    }

    //显示所有
    public void showAll() {
        int count = getChildCount();
        for (int i = 0; i < count; i++) {
            getChildAt(i).setVisibility(VISIBLE);
        }
        isShowAll = true;
    }

    //只显示指定行数
    public void showLines(int line) {
        int count = getChildCount();
        if (line >= count) {
            return;
        }
        for (int i = line; i < count; i++) {
            getChildAt(i).setVisibility(GONE);
        }
        isShowAll = false;
    }

    //判断是否大于指定行数
    public boolean isMoreThanLines(int line) {
        return getChildCount() > line;
    }

//    /**
//     * @param items
//     * @param onItemClick
//     */
//    public void initViews(List<String> items, OnItemClick onItemClick) {
//        initViews((String[]) items.toArray(), onItemClick);
//    }

    /**
     * 得到view控件的宽度
     *
     * @param view
     * @return
     */
    private int getViewWidth(View view) {
        int w = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
        int h = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
        view.measure(w, h);
        return view.getMeasuredWidth();
    }

    public interface OnItemClick {
        void onClick(int position);
    }

    public interface OnItemClickListenerV2 {
        void onClick(TipsItem item, boolean isCheck);
    }

    public void setIsSingleCheck(boolean isSingleCheck) {
        this.isSingleCheck = isSingleCheck;
    }

    public boolean getIsSingleCheck() {
        return isSingleCheck;
    }

    public void setLr(int big, int small) {
        lr_big = big;
        lr_small = small;
    }

    public void setLeftMargin(int leftMargin) {
        this.leftMargin = leftMargin;
    }
}
